#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

using namespace std;

#define MY_FIFO "./MY_FIFO"

int main()
{
    //以写的方式打开管道
    int write_fd = open(MY_FIFO, O_WRONLY);
    if (write_fd == -1)
    {
        perror("open fail");
        return 1;
    }

    //业务逻辑
    while(1)
    {
        cout << "请输入 : ";
        fflush(stdout);
        
        char buffer[64] = {0};
        //把标准输入的数据,拿到client进程内部
        ssize_t s = read(0, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            //这里如果是buffer[s] = 0,那么就会把标准输入的换行等符号给读取到,所以这里设置为buffer[s-1] = 0,规避掉换行符
            buffer[s - 1] = '\0';
            cout << buffer << endl;

            //将获取到的数据写入管道
            write(write_fd, buffer, strlen(buffer));
        }
    }
    close(write_fd);
    return 0;
}
